$(document).ready(function(){
	$("#episode_list dl.ep dd").hide();
	$("#unseen_episode_list dl.ep dd").hide();

	$("#episode_list dl dt").click(function(){
		var $this = $(this);
		if( $this.parent().is('.selected') ) {
			$this.next().slideUp();
			$this.parent().removeClass('selected');
		}
		else {
			$("#episode_list dd").slideUp();
			$("#episode_list dl").removeClass('selected');
			$this.next().slideDown();
			$this.parent().addClass('selected');
		}
	});
	$("#unseen_episode_list dl dt").click(function(){
		var $this = $(this);
		if( $this.parent().is('.selected') ) {
			$this.next().slideUp();
			$this.parent().removeClass('selected');
		}
		else {
			$("#unseen_episode_list dd").slideUp();
			$("#unseen_episode_list dl").removeClass('selected');
			$this.next().slideDown();
			$this.parent().addClass('selected');
		}
	});

	$(".delete_show").click(function(){
		if (!confirm("Are you sure you want to delete this Show?\nAll Seasons and Episodes will also be deleted!!!")){
			return false;
		}
		else {
			if (!confirm("Are you really, REALLY sure?")){
				return false;
			}	
		}
	});
	$(".delete_season").click(function(){
		if (!confirm("Are you sure you want to delete this Season?\nAll of it\'s Episodes will also be deleted!!!")){
			return false;
		}
		else {
			if (!confirm("Are you really, REALLY sure?")){
				return false;
			}	
		}
	});
	$(".delete_episode").click(function(){
		if (!confirm("Are you sure you want to delete this Episode?")){
			return false;
		}
	});
	$(".mark_all_seen").click(function(){
		if (!confirm("Are you sure you want to mark All episodes of this Season as Seen?")){
			return false;
		}
	});
	$("form#add input:first").focus();
	
	$("input[type=file]").stylefile({ 
	    image: "library/images/browse.png",
	    imageheight : 25,
	    imagewidth : 65,
	    width : 200
	});
	
	// validate form when it is submitted
	$("#add").validate();
	$("#add_show").validate();
	$("#edit").validate();
	$("#import").validate();
	
	
	$.extend($.blockUI.defaults.pageMessageCSS, { border:'none' });
	
	$("#import_notice").hide();
	$("#add").submit(function(){
		if ($("#import").is(":checked")) {
			var messageElement = $('#import_notice'); 
			$.blockUI(messageElement);
		}
	});
	$("#add_show").submit(function(){
		if ($("#import").is(":checked")) {
			var messageElement = $('#import_notice'); 
			$.blockUI(messageElement);
		}
	});
	
	$("#get").click(function(){
		if( $('input#episode_nr').val() > 0 ) {
			var messageElement = $('#import_notice'); 
			$.blockUI(messageElement);
		}
	});
	
	$("#import").submit(function(){
		var messageElement = $('#import_notice'); 
		$.blockUI(messageElement);
	});
	
	// Quicksearch unseen episodes
	$('#unseen_episode_list dl').quicksearch({
		attached: '#show2',
		position: 'append',
		labelText: 'Filter by:',
		inputText: '',
		loaderText: 'Applying filter...'
	});
	
	// Check All Checkboxes
	$('#checkboxall').click(function() {
		$('input[@type=checkbox]').not('#checkboxall').each(function() {
		this.checked = "checked";
		});
	});
	
	$("#show_name").autocomplete(shows, {
		matchContains: true,
		minChars: 2
	});
	
});